package com.winenine999.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.custom.dao.CWinebrandMapper;
import com.winenine999.model.Page;
import com.winenine999.model.Winebrand;
import com.winenine999.model.Winery;
import com.winenine999.model.Winetype;
import com.winenine999.service.IWinebrandService;
import com.winenine999.service.IWineproductService;
import com.winenine999.service.IWineryService;
import com.winenine999.service.IWinetypeService;

@Controller
@RequestMapping(value="/winebrand")
public class WinebrandController {
	@Autowired
	private IWineryService wineryService;
	@Autowired
	private IWinetypeService winetypeService;
	@Autowired
	private IWineproductService wineproductService;
	@Autowired
	private IWinebrandService winebrandService;
	/**
	 * 进入winetype酒类型主页面
	 * @return
	 */
	@RequestMapping(value="/winebrand",method=RequestMethod.GET)
	public String winery(){
		return "wine/winebrand";
	}
	/**
	 * 分页查询winebrands，给datatables返回值
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/winebrand_list_datatables",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> winebrand_list_datatables(Integer start,Integer length){
		Map<String,Object> map=new HashMap<String,Object>();
		Page page1=winebrandService.findPageWinebrands(new Page(start,length));
		map.put("aaData", page1.getList());//查到的结果集
		map.put("recordsFiltered", page1.getTotal());//总条数，用于计算页数
		map.put("recordsTotal",  page1.getTotal());//总条数，用于页面显示：‘全部记录数  条’
		return map;
	}
	/**
	 * 添加winebrand
	 * @param winebrand
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add(Winebrand winebrand){
		Integer result=winebrandService.insertSelective(winebrand);
		return result;
	}
	/**
	 * 通过id拿到修改的数据填充修改弹框
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/updatePre",method=RequestMethod.POST)
	@ResponseBody
	public Winebrand updatePre(Integer id){
		Winebrand winebrand=winebrandService.selectByPrimaryKey(id);
		return winebrand;
	}
	/**
	 * 修改winebrand
	 * @param winebrand
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Integer update(Winebrand winebrand){
		Integer result=winebrandService.updateByPrimaryKeySelective(winebrand);
		return result;
	}
	/**
	 * 根据id删除winebrand
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		boolean flag=true;
		List<Map<String,Object>> maps=wineproductService.selectAllWineProducts();
		for(Map<String,Object> map:maps){
			if(map.get("typeid").equals(id)){
				result=2;
				flag=false;
				break;
			}
			 
		}
		if(flag){			
			result=winebrandService.deleteByPrimaryKey(id);
		}
		return result;
	}
}
